﻿package com.foliopress 
{
	import flash.display.*;
	import flash.errors.IllegalOperationError;
	/**
	 * A static class that holds a reference to the main class, the stage instance of the main class
	 */
	public class Global 
	{
		/**
		* Determines whether debug printout is enabled. It also prints version information at startup.
		*/
		public static var VERBOSE:Boolean = true;
		/**
		 * error message to throw if main instance is assigned 
		 */
		private static const NOT_ASSIGNED_ERROR:String = "The main instance has not been assgined";
		/**
		 * error message to throw if main instance is assigned again
		 */
		private static const DUPLICATE_ASSIGNMENT_ERROR:String = "The main instance is already assigned";
		/**
		 * reference to main class, easier to access
		 */
		private static var _main:MovieClip;		
		/**
		 * return the main instance
		 */
		public static function get main():MovieClip 
		{
			if (!_main)
			{
				throw new IllegalOperationError(NOT_ASSIGNED_ERROR);
				return;
			}
			return _main
		}
		/**
		 * set the main instance, if main instance already assigned shows error
		 */
		public static function set main(instance:MovieClip):void 
		{
			if (!_main) {
				_main = instance;
			}else {
				throw new IllegalOperationError(DUPLICATE_ASSIGNMENT_ERROR);
			}
		}
		/**
		 * return the stage of main class
		 */
		public static function get stage():Stage
		{
			if (!_main)
			{
				throw new IllegalOperationError(NOT_ASSIGNED_ERROR);
			}
			return _main.stage;
		}
		/**
		* Sends debug information to the Output panel.
		* @param	message			A String value to send to Output.
		*/
		public static function log( message :String ):void
		{
			if( VERBOSE ){
				trace( message );
			}
		}
		/**
		 * Determine whether the swf file is served over web server or running from local machine<br>
		 * The check is to make sure not to breach local security restrictions<br>
		 * If the MIAN swf loaderInfo.url start with "http", the curent is running in a web server.
		 */
		public static function get isOnWeb():Boolean 
		{
			if (!_main)
			{
				throw new IllegalOperationError(NOT_ASSIGNED_ERROR);
				return;
			}
			var url:String = _main.loaderInfo.url;
			return url.indexOf("http") == 0;
		}
		
	}
	
}